package com.wwj.study.proxy.dynamics;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by lenovo on 2018/11/17.
 */
public class CgLibMeipo implements MethodInterceptor{
    public Object getInstance(Class<?> clazz){
        Enhancer enhancer=new Enhancer();
        //继承哪个对象
        enhancer.setSuperclass(clazz);

        //回调方法，回调自己
        enhancer.setCallback(this);

        //创建一个子类
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("开始物色");
        methodProxy.invokeSuper(o,objects);
        System.out.println("找到了");
        return null;
    }
}
